package com.nju.edu.erp.web.controller;


import com.nju.edu.erp.auth.Authorized;
import com.nju.edu.erp.enums.Role;
import com.nju.edu.erp.enums.sheetState.SalarySheetState;
import com.nju.edu.erp.model.vo.UserVO;
import com.nju.edu.erp.model.vo.salary.SalarySheetVO;
import com.nju.edu.erp.service.finance.FinanceSalaryService;
import com.nju.edu.erp.web.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path = "/finance-salary")
public class  FinanceSalaryController {

    private final FinanceSalaryService financeSalaryService;

    @Autowired
    public FinanceSalaryController(FinanceSalaryService financeSalaryService) {
        this.financeSalaryService = financeSalaryService;
    }

    @Authorized(roles = {Role.FINANCIAL_STAFF, Role.GM, Role.ADMIN})
    @PostMapping(value = "/sheet-make")
    public Response makeSalaryOrder(UserVO userVO, @RequestBody SalarySheetVO salarySheetVO) {
        financeSalaryService.makeSalarySheet(userVO, salarySheetVO);
        return Response.buildSuccess();
    }

    @GetMapping(value = "/sheet-show")
    public Response showSheetByState(@RequestParam(value = "state", required = false) SalarySheetState state)  {
        return Response.buildSuccess(financeSalaryService.getSalarySheetByState(state));
    }

    @Authorized (roles = {Role.GM, Role.ADMIN})
    @GetMapping(value = "/approval")
    public Response Approval(@RequestParam("salarySheetId") String salarySheetId,
                             @RequestParam("state") SalarySheetState state)  {
        if(state.equals(SalarySheetState.FAILURE) || state.equals(SalarySheetState.SUCCESS)) {
            financeSalaryService.approval(salarySheetId, state);
            return Response.buildSuccess();
        } else {
            return Response.buildFailed("000000","操作失败"); // code可能得改一个其他的
        }
    }

}
